<?php
/**
 * Created by PhpStorm.
 * User: fei
 * Date: 2016/8/23
 * Time: 17:28
 */

namespace Admin\Controller;
//use Think\Controller ;
use Component\AdminController;


class KejixiangmuController extends AdminController                                                                                                                                                                                            
{
   //增加企业科技项目概况信息
    public function append(){
         $rinfo = $this -> getComInfo();
          $this -> assign('rinfo',$rinfo);


        if(IS_POST) {
            $model = M('gq005');

            $GQ006_ID = I('post.GQ006_ID');
            if (empty($GQ006_ID)) {
                // echo '企业单位详细名称不能为空!' . "";
                echo "<script type='text/javascript'>alert('企业单位详细名称不能为空!数据添加失败！');history.back(-1);</script>";
            } else {


                $data = $model->create();
                $res = $model->add();
                if ($res) {
                    $info = $model->select();
                    $this->assign("info", $info);
                    $this->display("append");
                    echo "<script type='text/javascript'>{alert('数据添加成功');}</script>";
//                echo "<script type='text/javascript'>alert('数据添加成功');history.back(-1);</script>";
                } else {
                    echo '数据添加失败';
                }
                return;
            }
        }
        $this->display();
    }

//获取所有企业名称
 function getComInfo(){
        //查询全部企业的信息
        $rrinfo = D("gq006")->select(); //二维数组信息
        //array(1=>'经理',2=>'主管',3=>'总监')
        $rinfo = array();
        foreach($rrinfo as $k => $v){
            $rinfo[$v['GQ006_ID']] = $v['QA04'];//array(1=>'经理',2=>'主管')
        }
        return $rinfo;
    }

    //查询当前企业科技项目概况
    function showlist_kjxm($GQ006_ID){
        $model = M('gq005');
        $rinfo = $this ->getComInfo();
        $this -> assign('rinfo',$rinfo);

        $sql = "select * from gq005 where GQ006_ID=$GQ006_ID";
//print_r($sql);
        $info = $model->query($sql);
        //print_r($info);

        $this -> assign('info', $info);
        $this -> display();

    }

//查询所有企业科技项目概况
    function showlist(){

        $model = D("gq005");



         $rinfo = $this ->getComInfo();
         $this -> assign('rinfo',$rinfo);

        //1. 获得当前记录总条数
        $total = $model -> count();
        $per = 7;
        //2. 实例化分页类对象
        $page = new \Component\Page($total, $per); //autoload
        //3. 拼装sql语句获得每页信息
        $sql = "select * from gq005 ".$page->limit;
        $info = $model -> query($sql);
        //4. 获得页码列表
        $pagelist = $page -> fpage();

        $this -> assign('info', $info);
        $this -> assign('pagelist', $pagelist);
        $this -> display();
    }
    //修改企业科技项目概况
    function upd($GQ005_ID){
        //查询被修改商品的信息并传递给模板展示
        $model = D("gq005");
        //两个逻辑：展示表单、收集表单
        if(IS_POST){
            $model -> create();
            $rst = $model -> save();
            if($rst){
                echo "<script type='text/javascript'>alert('数据修改成功');history.back(-1);</script>";;
            } else {
                echo "<script type='text/javascript'>alert('数据修改失败');history.back(-1);</script>";
            }
        } else {
            $info = $model->find($GQ005_ID); //一维数组
            $this -> assign('info', $info);

             $rinfo = $this ->getComInfo();
               $this -> assign('rinfo',$rinfo);


            $this -> display();
        }
    }
    //删除企业科技项目概况
    function del($GQ005_ID)
    {
        //查询被修改商品的信息并传递给模板展示
        $model = D("gq005");

        $res = $model->delete($GQ005_ID);

        if ($res) {
            //echo "<script type='text/javascript'>window.onload=function(){alert('数据删除成功');}</script>";
//            $info = $model->select();
//            $this->assign("info", $info);
//            $this->display("showlist");
            echo "<script type='text/javascript'>{alert('数据删除成功');history.back(-1);}</script>";
            //$this->showlist();
            //$this->success('删除成功');

        } else {
            echo "<script type='text/javascript'>alert('数据删除失败');history.back(-1);</script>";
        }
    }

    //导出企业科技项目概况表格
    public function output()
    {
//获取所需数据
        $res = M('gq005')->select();

        $rinfo = $this->getComInfo();
        $this->assign('rinfo', $rinfo);



//        $export = I('export');//触发导出指令的值
//        if ($export ==１){
        if (IS_POST) {


            $xlsName = "企业科技项目概况列表";//表名
            $xlsCell = array(
                array('QA04', '企业单位详细名称'),
                array('QH00', '项目序号（QH00）') ,
                array('xmmc', '项目名称（xmmc）') ,
                array('QH11', '项目来源(QH11)'),

                array('QH31', '项目合作形式（QH31）'),
                array('QH21', '项目成果形式(QH21)'),
                array('QH32', '项目技术经济目标（QH32）'),
                array('QH33', '项目活动类型(QH33)'),
                array('QH34', '项目起始日期（QH34）'),

                array('QH35', '项目完成日期(QH35)'),
                array('QH36', '跨年项目所处进展阶段(QH36)'),
                array('QH44', '参加项目人员（QH44）（人）'),
                array('QH40', '项目人员实际工作时间(QH40)（人月）'),
                array('QH51', '项目经费内部支出(QH51)（千元）'),





            );
            $xlsData = array();
            foreach ($res as $key => $val) {
                array_push($xlsData, array(//这里的需要导出的内容，要注意键名跟上面的字段键名要一致
                    // 'QA04' => $val[GQ006_ID],
                    'QA04' => $rinfo[$val[GQ006_ID]],
                    'QH00' => $val[QH00],
                    'xmmc' => $val[xmmc],
                    'QH11' => $val[QH11],

                    'QH31' => $val[QH31],
                    'QH21' => $val[QH21],
                    'QH32' => $val[QH32],
                    'QH33' => $val[QH33],
                    'QH34' => $val[QH34],

                    'QH35' => $val[QH35],
                    'QH36' => $val[QH36],
                    'QH44' => $val[QH44],
                    'QH40' => $val[QH40],
                    'QH51' => $val[QH51],

                  



                ));
            }

            exportExcel($xlsName, $xlsCell, $xlsData);//这里就是调用写在function里的函数了。
        }
        die();
    }



}